﻿using System;
using System.IO;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace helicopterFight
{
    class MenuState : State
    {
        AudioEngine audioEngine;
        WaveBank waveBank;
        public static SoundBank soundBank;

        public static Cue bgrMusic;
        public static Cue gameMusic;
        public static AudioCategory soundCategory, musicCategory, gamemusicCategory;

        private String nazwa = "Menu";
        Game1 game = null;
        private Console konsola = Game1.konsola;
        SpriteFont font1 = null;
        Vector2 selectedPos = new Vector2(270, 200);
        KeyboardState oldKeybState;

        int changeScreen = 1;
        int terrainActual = 0;
        bool    initial = true;
        Texture2D tex, tex2, terrain1,terrain2, left_button, right_button, full_on, full_off;

        public static int volumeLevelSound, 
                          volumeLevelMMusic,
                          volumeLevelGMusic;

        public static float soundVolume = 5.0f,
                            musicVolume = 4.0f,
                            gameVolume = 4.0f;

        const int NUMBER_OF_TEX = 2,
                  NUMBER_OF_BUTTONS = 16,
                  SINGLEPLAYER_BUTTON = 0,
                  MULTIPLAYER_BUTTON = 1,
                  OPTIONS_BUTTON = 2,
                  EXIT_BUTTON = 3,
                  PLAY_BUTTON = 4,
                  SOUND_BUTTON = 5,
                  CONTROLS_BUTTON = 6,
                  JOIN_BUTTON = 7,
                  CREATE_BUTTON = 8,
                  SFX_BUTTON = 9,
                  MENUMUSIC_BUTTON = 10,
                  GAMEMUSIC_BUTTON = 11,
                  SCREENRES_BUTTON = 12,
                  APPLYRES_BUTTON = 13,
                  REDEFINE_BUTTON = 14,
                  FULLSCREEN_BUTTON = 15,

                  VOLUME_1 = 0,
                  VOLUME_2 = 1,
                  VOLUME_3 = 2,
                  VOLUME_4 = 3,
                  VOLUME_5 = 4,

                  CON_ENGINE = 0,
                  CON_FLYUP = 1,
                  CON_FLYDOWN = 2,
                  CON_FLYFORWARD = 3,
                  CON_FLYBACKWARD = 4,
                  CON_FLYLEFT = 5,
                  CON_FLYRIGHT = 6,
                  CON_SHOOT = 7,

                  RES800x600 = 0,
                  RES1024x768 = 1,
                  RES1280x768 = 2,

                  MENU_BUTTON_HEIGHT = 59,
                  MENU_BUTTON_WIDTH = 140;

        Texture2D[] button_tex = new Texture2D[NUMBER_OF_BUTTONS];
        Rectangle[] button_rectangle = new Rectangle[NUMBER_OF_BUTTONS];
        Color[] button_color = new Color[NUMBER_OF_BUTTONS]; //kolory 

        Texture2D[] volume_tex = new Texture2D[5];
        Color[]  volumeSound_color = new Color[5];
        Color[] volumeMMusic_color = new Color[5]; // menu music
        Color[] volumeGMusic_color = new Color[5]; //game music

        Texture2D[] controls_tex = new Texture2D[8];
        Color[] controls_color = new Color[8];

        Terrain[] levels = new Terrain[NUMBER_OF_TEX];

        Texture2D[] resolutions = new Texture2D[3]; //800x600, 1024x768, 1280x768
        //Color[] resolutions_color = new Color[3];
        int buttonUp; //aktualnie podswietlony

        int resolutionUp = 0;
        bool resApplied = false;
        bool fullscreen = false;

        // METODY ===========================

        public void setContent(Game1 gam)
        {
            game = gam;
        }

        public String getNazwa()
        {
            return nazwa;
        }

        public void loadContent()
        {
            font1 = game.Content.Load<SpriteFont>("fonts/SpriteFont1");
            tex = game.Content.Load<Texture2D>("bgr/flame");
            tex2 = game.Content.Load<Texture2D>("bgr/flame2");

            terrain1 = game.Content.Load<Texture2D>("bgr/terrain_1");
            terrain2 = game.Content.Load<Texture2D>("bgr/terrain_2");

            button_tex[SINGLEPLAYER_BUTTON] = game.Content.Load<Texture2D>("bgr/singleplayer");
            button_tex[MULTIPLAYER_BUTTON] = game.Content.Load<Texture2D>("bgr/multiplayer");
            button_tex[OPTIONS_BUTTON] = game.Content.Load<Texture2D>("bgr/options");
            button_tex[EXIT_BUTTON] = game.Content.Load<Texture2D>("bgr/exit");
            button_tex[PLAY_BUTTON] = game.Content.Load<Texture2D>("bgr/play");
            button_tex[SOUND_BUTTON] = game.Content.Load<Texture2D>("bgr/sound");
            button_tex[CONTROLS_BUTTON] = game.Content.Load<Texture2D>("bgr/controls");
            button_tex[JOIN_BUTTON] = game.Content.Load<Texture2D>("bgr/join");
            button_tex[CREATE_BUTTON] = game.Content.Load<Texture2D>("bgr/create");
            button_tex[SFX_BUTTON] = game.Content.Load<Texture2D>("bgr/sfx");
            button_tex[MENUMUSIC_BUTTON] = game.Content.Load<Texture2D>("bgr/menumusic");
            button_tex[GAMEMUSIC_BUTTON] = game.Content.Load<Texture2D>("bgr/gamemusic");
            button_tex[SCREENRES_BUTTON] = game.Content.Load<Texture2D>("bgr/screenres");
            button_tex[APPLYRES_BUTTON] = game.Content.Load<Texture2D>("bgr/applyres");
            button_tex[REDEFINE_BUTTON] = game.Content.Load<Texture2D>("bgr/redefine");
            button_tex[FULLSCREEN_BUTTON] = game.Content.Load<Texture2D>("bgr/fullscreen");

            left_button = game.Content.Load<Texture2D>("bgr/left");
            right_button = game.Content.Load<Texture2D>("bgr/right");

            levels[0] = new Terrain(terrain1, "Sandbox", "Hard as Hell", "2:00");
            levels[1] = new Terrain(terrain2, "Level 2", "Pretty Easy", "4:00");

            volume_tex[VOLUME_1] = game.Content.Load<Texture2D>("bgr/volume_1_10x20");
            volume_tex[VOLUME_2] = game.Content.Load<Texture2D>("bgr/volume_2_10x30");
            volume_tex[VOLUME_3] = game.Content.Load<Texture2D>("bgr/volume_3_10x40");
            volume_tex[VOLUME_4] = game.Content.Load<Texture2D>("bgr/volume_4_10x50");
            volume_tex[VOLUME_5] = game.Content.Load<Texture2D>("bgr/volume_5_10x60");

            resolutions[RES800x600] = game.Content.Load<Texture2D>("bgr/800x600");
            resolutions[RES1024x768] = game.Content.Load<Texture2D>("bgr/1024x768");
            resolutions[RES1280x768] = game.Content.Load<Texture2D>("bgr/1280x768");

            full_on = game.Content.Load<Texture2D>("bgr/full_on");
            full_off = game.Content.Load<Texture2D>("bgr/full_off");

            controls_tex[CON_ENGINE] = game.Content.Load<Texture2D>("bgr/con_engine");
            controls_tex[CON_FLYUP] = game.Content.Load<Texture2D>("bgr/con_flyup");
            controls_tex[CON_FLYDOWN] = game.Content.Load<Texture2D>("bgr/con_flydown");
            controls_tex[CON_FLYFORWARD] = game.Content.Load<Texture2D>("bgr/con_flyforward");
            controls_tex[CON_FLYBACKWARD] = game.Content.Load<Texture2D>("bgr/con_flybackward");
            controls_tex[CON_FLYLEFT] = game.Content.Load<Texture2D>("bgr/con_flyleft");
            controls_tex[CON_FLYRIGHT] = game.Content.Load<Texture2D>("bgr/con_flyright");
            controls_tex[CON_SHOOT] = game.Content.Load<Texture2D>("bgr/con_shoot");
        }
       

        public void update()
        {

            KeyboardState ks = Keyboard.GetState();
            if (ks.IsKeyDown(Keys.Down) && !oldKeybState.IsKeyDown(Keys.Down))
            {
                
                if (changeScreen == 1) 
                { 
                    if (buttonUp < EXIT_BUTTON)
                    {
                        soundBank.PlayCue("switch");
                        button_color[buttonUp] = Color.White;
                        buttonUp++;
                        button_color[buttonUp] = Color.Yellow;
                    }
                }
                
                if (changeScreen == 2) 
                {
                    if(buttonUp == PLAY_BUTTON)
                        soundBank.PlayCue("switch");
                    button_color[buttonUp] = Color.White;
                    buttonUp = EXIT_BUTTON;
                    button_color[buttonUp] = Color.Yellow;                     
                }

                if (changeScreen == 4) 
                { 
                    if(buttonUp == SCREENRES_BUTTON)
                    {
                        soundBank.PlayCue("switch");
                        button_color[buttonUp] = Color.White;
                        buttonUp = APPLYRES_BUTTON;
                        button_color[buttonUp] = Color.Yellow;   
                    }
                    else if (buttonUp == APPLYRES_BUTTON)
                    {
                        soundBank.PlayCue("switch");
                        button_color[buttonUp] = Color.White;
                        buttonUp = FULLSCREEN_BUTTON;
                        button_color[buttonUp] = Color.Yellow;
                    }
                    else if (buttonUp == CONTROLS_BUTTON)
                    {
                        soundBank.PlayCue("switch");
                        button_color[buttonUp] = Color.White;
                        buttonUp = SCREENRES_BUTTON;
                        button_color[buttonUp] = Color.Yellow;
                    }
                    else if (buttonUp == SOUND_BUTTON)
                    {
                        soundBank.PlayCue("switch");
                        button_color[buttonUp] = Color.White;
                        buttonUp = CONTROLS_BUTTON;
                        button_color[buttonUp] = Color.Yellow;
                    }
                    else if (buttonUp == FULLSCREEN_BUTTON)
                    {
                        soundBank.PlayCue("switch");
                        button_color[buttonUp] = Color.White;
                        buttonUp = EXIT_BUTTON;
                        button_color[buttonUp] = Color.Yellow;
                    }
                }

                if (changeScreen == 5)
                {
                    if (buttonUp == SFX_BUTTON)
                    {
                        soundBank.PlayCue("switch");
                        button_color[buttonUp] = Color.White;
                        buttonUp = MENUMUSIC_BUTTON;
                        button_color[buttonUp] = Color.Yellow;
                    }
                    else if (buttonUp == MENUMUSIC_BUTTON)
                    {
                        soundBank.PlayCue("switch");
                        button_color[buttonUp] = Color.White;
                        buttonUp = GAMEMUSIC_BUTTON;
                        button_color[buttonUp] = Color.Yellow;
                    }
                    else if (buttonUp == GAMEMUSIC_BUTTON)
                    {
                        soundBank.PlayCue("switch");
                        button_color[buttonUp] = Color.White;
                        buttonUp = EXIT_BUTTON;
                        button_color[buttonUp] = Color.Yellow;
                    }
                }
            }
            else if (ks.IsKeyDown(Keys.Up) && !oldKeybState.IsKeyDown(Keys.Up))
            {
                
                if (changeScreen == 1)
                {
                    if (buttonUp > 0)
                    {
                        soundBank.PlayCue("switch");
                        button_color[buttonUp] = Color.White;
                        buttonUp--;
                        button_color[buttonUp] = Color.Yellow;
                    }
                }
                if (changeScreen == 2) 
                {
                    if(buttonUp == EXIT_BUTTON)
                      soundBank.PlayCue("switch");
                    button_color[buttonUp] = Color.White;
                    buttonUp = PLAY_BUTTON;
                    button_color[buttonUp] = Color.Yellow;
                }

                if (changeScreen == 4) 
                {
                    if (buttonUp == CONTROLS_BUTTON)
                    {
                        soundBank.PlayCue("switch");
                        button_color[buttonUp] = Color.White;
                        buttonUp = SOUND_BUTTON;
                        button_color[buttonUp] = Color.Yellow;
                    }
                    else if (buttonUp == SCREENRES_BUTTON)
                    {
                        soundBank.PlayCue("switch");
                        button_color[buttonUp] = Color.White;
                        buttonUp = CONTROLS_BUTTON;
                        button_color[buttonUp] = Color.Yellow;
                    }
                    else if (buttonUp == APPLYRES_BUTTON)
                    {
                        soundBank.PlayCue("switch");
                        button_color[buttonUp] = Color.White;
                        buttonUp = SCREENRES_BUTTON;
                        button_color[buttonUp] = Color.Yellow;
                    }

                    else if (buttonUp == EXIT_BUTTON)
                    {
                        soundBank.PlayCue("switch");
                        button_color[buttonUp] = Color.White;
                        buttonUp = FULLSCREEN_BUTTON;
                        button_color[buttonUp] = Color.Yellow;
                    }
                    else if (buttonUp == FULLSCREEN_BUTTON)
                    {
                        soundBank.PlayCue("switch");
                        button_color[buttonUp] = Color.White;
                        buttonUp = APPLYRES_BUTTON;
                        button_color[buttonUp] = Color.Yellow;
                    }
                }

                if (changeScreen == 5)
                {
                    if (buttonUp == EXIT_BUTTON)
                    {
                        soundBank.PlayCue("switch");
                        button_color[buttonUp] = Color.White;
                        buttonUp = GAMEMUSIC_BUTTON;
                        button_color[buttonUp] = Color.Yellow;
                    }
                    else if (buttonUp == GAMEMUSIC_BUTTON)
                    {
                        soundBank.PlayCue("switch");
                        button_color[buttonUp] = Color.White;
                        buttonUp = MENUMUSIC_BUTTON;
                        button_color[buttonUp] = Color.Yellow;
                    }
                    else if (buttonUp == MENUMUSIC_BUTTON)
                    {
                        soundBank.PlayCue("switch");
                        button_color[buttonUp] = Color.White;
                        buttonUp = SFX_BUTTON;
                        button_color[buttonUp] = Color.Yellow;
                    }
                }
            }
            else if (ks.IsKeyDown(Keys.Right) && !oldKeybState.IsKeyDown(Keys.Right)) 
            {
                
                if(changeScreen == 2)
                    if (terrainActual < 1)
                    {
                        soundBank.PlayCue("switch");
                        terrainActual++;
                    }
                if (changeScreen == 3) 
                {
                    if (buttonUp == CREATE_BUTTON)
                    {
                        soundBank.PlayCue("switch");
                        button_color[buttonUp] = Color.White;
                        buttonUp = EXIT_BUTTON;
                        button_color[buttonUp] = Color.Yellow;
                    }

                    if (buttonUp == JOIN_BUTTON) 
                    {
                        soundBank.PlayCue("switch");
                        button_color[buttonUp] = Color.White;
                        buttonUp = CREATE_BUTTON;
                        button_color[buttonUp] = Color.Yellow;
                    }
                }
                if (changeScreen == 4 && buttonUp == SCREENRES_BUTTON)
                {
                    if (resolutionUp == RES800x600)
                        resolutionUp = RES1024x768;
                    else if (resolutionUp == RES1024x768)
                        resolutionUp = RES1280x768;
                }
                if (changeScreen == 4 && buttonUp == FULLSCREEN_BUTTON)
                {
                    if(!fullscreen)
                    {
                        game.graphics.IsFullScreen = true;
                        fullscreen = true;
                        game.graphics.ApplyChanges();
                    }
                }

                if (changeScreen == 5) 
                {
                    if (volumeLevelSound < 4 && buttonUp == SFX_BUTTON)
                    {
                        volumeLevelSound++;
                        volumeSound_color[volumeLevelSound] = Color.Red;
                        soundVolume += 2.5f;
                        soundCategory.SetVolume(soundVolume);
                    }

                    if (volumeLevelMMusic < 4 && buttonUp == MENUMUSIC_BUTTON)
                    {
                        volumeLevelMMusic++;
                        volumeMMusic_color[volumeLevelMMusic] = Color.Red;
                        musicVolume += 2.0f;
                        musicCategory.SetVolume(musicVolume);
                    }

                    if (volumeLevelGMusic < 4 && buttonUp == GAMEMUSIC_BUTTON)
                    {
                        volumeLevelGMusic++;
                        volumeGMusic_color[volumeLevelGMusic] = Color.Red;
                        gameVolume += 2.0f;
                        gamemusicCategory.SetVolume(gameVolume);
                    }

                }
            }
            else if (ks.IsKeyDown(Keys.Left) && !oldKeybState.IsKeyDown(Keys.Left))
            {
                
                if (changeScreen == 2)
                    if (terrainActual > 0)
                    {
                        soundBank.PlayCue("switch");
                        terrainActual--;
                    }
                if (changeScreen == 3)
                {
                    if (buttonUp == CREATE_BUTTON)
                    {
                        soundBank.PlayCue("switch");
                        button_color[buttonUp] = Color.White;
                        buttonUp = JOIN_BUTTON;
                        button_color[buttonUp] = Color.Yellow;
                    }

                    if (buttonUp == EXIT_BUTTON)
                    {
                        soundBank.PlayCue("switch");
                        button_color[buttonUp] = Color.White;
                        buttonUp = CREATE_BUTTON;
                        button_color[buttonUp] = Color.Yellow;
                    }
                }

                if (changeScreen == 4 && buttonUp == SCREENRES_BUTTON)
                {
                    if (resolutionUp == RES1280x768)
                        resolutionUp = RES1024x768;
                    else if (resolutionUp == RES1024x768)
                        resolutionUp = RES800x600;
                }
                if (changeScreen == 4 && buttonUp == FULLSCREEN_BUTTON)
                {
                    if (fullscreen)
                    {
                        game.graphics.IsFullScreen = false;
                        fullscreen = false;
                        game.graphics.ApplyChanges();
                    }
                }

                if (changeScreen == 5)
                {
                    if (volumeLevelSound > 0 && buttonUp == SFX_BUTTON)
                    {
                        volumeSound_color[volumeLevelSound] = Color.White;
                        volumeLevelSound--;
                        soundVolume -= 2.5f;
                        soundCategory.SetVolume(soundVolume);
                    }
                    if (volumeLevelMMusic > 0 && buttonUp == MENUMUSIC_BUTTON)
                    {
                        volumeMMusic_color[volumeLevelMMusic] = Color.White;
                        volumeLevelMMusic--;
                        musicVolume -= 2.0f;
                        musicCategory.SetVolume(musicVolume);
                    }
                    if (volumeLevelGMusic > 0 && buttonUp == GAMEMUSIC_BUTTON)
                    {
                        volumeGMusic_color[volumeLevelGMusic] = Color.White;
                        volumeLevelGMusic--;
                        gameVolume -= 2.0f;
                        gamemusicCategory.SetVolume(gameVolume);
                    }
                }
            }

            else if (ks.IsKeyDown(Keys.Enter) && !oldKeybState.IsKeyDown(Keys.Enter))
            {
                
                switch (buttonUp)
                {
                    case SINGLEPLAYER_BUTTON: //wejscie do menu plansz   
                                            soundBank.PlayCue("badbang");
                                            changeScreen = 2;
                                            button_color[buttonUp] = Color.White;
                                            buttonUp = PLAY_BUTTON;
                                            button_color[buttonUp] = Color.Yellow;
                                            break;

                    case MULTIPLAYER_BUTTON: //wejscie do lobby   
                                            soundBank.PlayCue("badbang");
                                            changeScreen = 3;
                                            button_color[buttonUp] = Color.White;
                                            buttonUp = EXIT_BUTTON;
                                            game.testState.teren.init(game, game.testState, 0);
                                            button_color[buttonUp] = Color.Yellow;  
                                            break;

                    case OPTIONS_BUTTON: soundBank.PlayCue("badbang");                                       
                                        changeScreen = 4;
                                        button_color[buttonUp] = Color.White;
                                        buttonUp = SOUND_BUTTON;
                                        button_color[buttonUp] = Color.Yellow;  
                                        break;

                    case EXIT_BUTTON: if (changeScreen == 1)
                                            game.Exit();
                                        else if (changeScreen == 6) 
                                        {
                                            soundBank.PlayCue("badbang");
                                            changeScreen = 4;
                                            button_color[buttonUp] = Color.White;
                                            buttonUp = SOUND_BUTTON;
                                            button_color[buttonUp] = Color.Yellow;
                                        }
                                        else if (changeScreen == 5)
                                        {
                                            soundBank.PlayCue("badbang");
                                            changeScreen = 4;
                                            button_color[buttonUp] = Color.White;
                                            buttonUp = SOUND_BUTTON;
                                            button_color[buttonUp] = Color.Yellow;
                                        }
                                        else
                                        {
                                            soundBank.PlayCue("badbang");
                                            changeScreen = 1;
                                            button_color[buttonUp] = Color.White;
                                            buttonUp = SINGLEPLAYER_BUTTON;
                                            button_color[buttonUp] = Color.Yellow;
                                        }
                                      break;

                    case PLAY_BUTTON: if (terrainActual == 0 || terrainActual == 1)
                                      {
                                          soundBank.PlayCue("badbang");
                                          bgrMusic.Pause();
                                          Game1.stan = "Sandbox";
                                          game.testState.teren.init(game, game.testState, terrainActual);
                                          gameMusic = soundBank.GetCue("przyczajka");
                                          gameMusic.Play();
                                          game.sm.zmienStan("Sandbox");
                                      }  
                                      break;

                    case SOUND_BUTTON: changeScreen = 5;
                                      soundBank.PlayCue("badbang");
                                      button_color[buttonUp] = Color.White;
                                      buttonUp = SFX_BUTTON;
                                      button_color[buttonUp] = Color.Yellow;
                                      break;
                    case CONTROLS_BUTTON: changeScreen = 6;
                                      soundBank.PlayCue("badbang");
                                      button_color[buttonUp] = Color.White;
                                      buttonUp = EXIT_BUTTON;
                                      button_color[buttonUp] = Color.Yellow;
                                      break;

                    case APPLYRES_BUTTON: if (resolutionUp == RES1280x768)
                                          {
                                              game.graphics.PreferredBackBufferWidth = 1280;
                                              game.graphics.PreferredBackBufferHeight = 768;
                                              game.graphics.ApplyChanges();
                                              resApplied = true;  
                                          }
                                          else if (resolutionUp == RES1024x768)
                                          {
                                              game.graphics.PreferredBackBufferWidth = 1024;
                                              game.graphics.PreferredBackBufferHeight = 768;
                                              game.graphics.ApplyChanges();
                                              resApplied = true;
                                          }
                                          else if (resolutionUp == RES800x600)
                                          {
                                              game.graphics.PreferredBackBufferWidth = 800;
                                              game.graphics.PreferredBackBufferHeight = 600;
                                              game.graphics.ApplyChanges();
                                              resApplied = true;
                                          }
                                          break;
                  
                }
            }
            else if (ks.IsKeyDown(Keys.F1) && !oldKeybState.IsKeyDown(Keys.F1)) 
            {
                soundCategory.SetVolume(0.00f);
                musicCategory.SetVolume(0.00f);
                gamemusicCategory.SetVolume(0.00f);
            }
            else if (ks.IsKeyDown(Keys.F2) && !oldKeybState.IsKeyDown(Keys.F2))
            {
                soundCategory.SetVolume(volumeLevelSound);
                musicCategory.SetVolume(volumeLevelMMusic);
                gamemusicCategory.SetVolume(volumeLevelGMusic);
            }
            oldKeybState = ks;
        }

        public void drawSingle() {
            game.spriteBatch.Draw(button_tex[PLAY_BUTTON], new Rectangle(Convert.ToInt32(0.75 * game.graphics.PreferredBackBufferWidth), Convert.ToInt32(0.716 * game.graphics.PreferredBackBufferHeight), 140, 59), button_color[PLAY_BUTTON]);
            game.spriteBatch.Draw(button_tex[EXIT_BUTTON], new Rectangle(Convert.ToInt32(0.75 * game.graphics.PreferredBackBufferWidth), Convert.ToInt32(0.833 * game.graphics.PreferredBackBufferHeight), 140, 59), button_color[EXIT_BUTTON]);

            game.spriteBatch.Draw(left_button, new Rectangle(Convert.ToInt32(0.0375 * game.graphics.PreferredBackBufferWidth), Convert.ToInt32(0.866 * game.graphics.PreferredBackBufferHeight), 200, 57), Color.White);
            game.spriteBatch.Draw(right_button, new Rectangle(Convert.ToInt32(0.3125 * game.graphics.PreferredBackBufferWidth), Convert.ToInt32(0.866 * game.graphics.PreferredBackBufferHeight), 200, 57), Color.White);


            game.spriteBatch.Draw(levels[terrainActual].getMiniTex(), new Rectangle(Convert.ToInt32(0.0625 * game.graphics.PreferredBackBufferWidth), Convert.ToInt32(0.133 * game.graphics.PreferredBackBufferHeight), Convert.ToInt32(0.25 * game.graphics.PreferredBackBufferWidth), Convert.ToInt32(0.25 *game.graphics.PreferredBackBufferHeight)), Color.White);

            game.spriteBatch.DrawString(font1, "Map name:", new Vector2(Convert.ToInt32(0.0625 * game.graphics.PreferredBackBufferWidth), Convert.ToInt32(0.5 * game.graphics.PreferredBackBufferHeight)), Color.Black);
            game.spriteBatch.DrawString(font1, levels[terrainActual].getName(), new Vector2(Convert.ToInt32(0.2 * game.graphics.PreferredBackBufferWidth), Convert.ToInt32(0.5 * game.graphics.PreferredBackBufferHeight)), Color.Black);

            game.spriteBatch.DrawString(font1, "Difficulty:", new Vector2(Convert.ToInt32(0.0625 * game.graphics.PreferredBackBufferWidth), Convert.ToInt32(0.533 * game.graphics.PreferredBackBufferHeight)), Color.Black);
            game.spriteBatch.DrawString(font1, levels[terrainActual].getDifficulty(), new Vector2(Convert.ToInt32(0.2 * game.graphics.PreferredBackBufferWidth), Convert.ToInt32(0.533 * game.graphics.PreferredBackBufferHeight)), Color.Black);

            game.spriteBatch.DrawString(font1, "Mission time:", new Vector2(Convert.ToInt32(0.0625 * game.graphics.PreferredBackBufferWidth), Convert.ToInt32(0.566 * game.graphics.PreferredBackBufferHeight)), Color.Black);
            game.spriteBatch.DrawString(font1, levels[terrainActual].getMissionTime(), new Vector2(Convert.ToInt32(0.2 * game.graphics.PreferredBackBufferWidth), Convert.ToInt32(0.566 * game.graphics.PreferredBackBufferHeight)), Color.Black);

        }

        public void drawOptions() {
 
            game.spriteBatch.Draw(button_tex[SOUND_BUTTON], button_rectangle[0], button_color[SOUND_BUTTON]);
            game.spriteBatch.Draw(button_tex[CONTROLS_BUTTON], button_rectangle[1], button_color[CONTROLS_BUTTON]);
            game.spriteBatch.Draw(button_tex[SCREENRES_BUTTON], button_rectangle[2], button_color[SCREENRES_BUTTON]);
            int xx = button_rectangle[2].X;
            int yy = button_rectangle[2].Y;
            int yyy = button_rectangle[3].Y;
            if (resolutionUp == RES800x600)
                game.spriteBatch.Draw(resolutions[RES800x600], new Rectangle(xx + 170, yy, 140, 59), Color.White);

            if (resolutionUp == RES1024x768)         
                game.spriteBatch.Draw(resolutions[RES1024x768], new Rectangle(xx + 170, yy, 140, 59), Color.White);

            if (resolutionUp == RES1280x768)
                game.spriteBatch.Draw(resolutions[RES1280x768], new Rectangle(xx + 170, yy, 140, 59), Color.White);
            game.spriteBatch.Draw(button_tex[APPLYRES_BUTTON], new Rectangle(xx + 170, yyy, 140, 59), button_color[APPLYRES_BUTTON]);
            game.spriteBatch.Draw(button_tex[FULLSCREEN_BUTTON], button_rectangle[4], button_color[FULLSCREEN_BUTTON]);
            xx = button_rectangle[4].X;
            yy = button_rectangle[4].Y;
            if (fullscreen)
                game.spriteBatch.Draw(full_on, new Rectangle(xx + 170, yy, 140, 59), Color.White);  
            else
                game.spriteBatch.Draw(full_off, new Rectangle(xx + 170, yy, 140, 59), Color.White);
            game.spriteBatch.Draw(button_tex[EXIT_BUTTON], button_rectangle[5], button_color[EXIT_BUTTON]);
        }

        public void drawSound()
        {
            game.spriteBatch.Draw(button_tex[EXIT_BUTTON], new Rectangle(600, 515, 140, 59), button_color[EXIT_BUTTON]);


            game.spriteBatch.Draw(button_tex[SFX_BUTTON], new Rectangle(190, 145, 140, 59), button_color[SFX_BUTTON]);
            game.spriteBatch.Draw(volume_tex[VOLUME_1], new Rectangle(400, 190, 7, 10), volumeSound_color[VOLUME_1]);
            game.spriteBatch.Draw(volume_tex[VOLUME_2], new Rectangle(410, 180, 7, 20), volumeSound_color[VOLUME_2]);
            game.spriteBatch.Draw(volume_tex[VOLUME_3], new Rectangle(420, 170, 7, 30), volumeSound_color[VOLUME_3]);
            game.spriteBatch.Draw(volume_tex[VOLUME_4], new Rectangle(430, 160, 7, 40), volumeSound_color[VOLUME_4]);
            game.spriteBatch.Draw(volume_tex[VOLUME_5], new Rectangle(440, 150, 7, 50), volumeSound_color[VOLUME_5]);

            game.spriteBatch.Draw(button_tex[MENUMUSIC_BUTTON], new Rectangle(190, 245, 140, 59), button_color[MENUMUSIC_BUTTON]);
            game.spriteBatch.Draw(volume_tex[VOLUME_1], new Rectangle(400, 290, 7, 10), volumeMMusic_color[VOLUME_1]);
            game.spriteBatch.Draw(volume_tex[VOLUME_2], new Rectangle(410, 280, 7, 20), volumeMMusic_color[VOLUME_2]);
            game.spriteBatch.Draw(volume_tex[VOLUME_3], new Rectangle(420, 270, 7, 30), volumeMMusic_color[VOLUME_3]);
            game.spriteBatch.Draw(volume_tex[VOLUME_4], new Rectangle(430, 260, 7, 40), volumeMMusic_color[VOLUME_4]);
            game.spriteBatch.Draw(volume_tex[VOLUME_5], new Rectangle(440, 250, 7, 50), volumeMMusic_color[VOLUME_5]);
  
            game.spriteBatch.Draw(button_tex[GAMEMUSIC_BUTTON], new Rectangle(190, 345, 140, 59), button_color[GAMEMUSIC_BUTTON]);
            game.spriteBatch.Draw(volume_tex[VOLUME_1], new Rectangle(400, 390, 7, 10), volumeGMusic_color[VOLUME_1]);
            game.spriteBatch.Draw(volume_tex[VOLUME_2], new Rectangle(410, 380, 7, 20), volumeGMusic_color[VOLUME_2]);
            game.spriteBatch.Draw(volume_tex[VOLUME_3], new Rectangle(420, 370, 7, 30), volumeGMusic_color[VOLUME_3]);
            game.spriteBatch.Draw(volume_tex[VOLUME_4], new Rectangle(430, 360, 7, 40), volumeGMusic_color[VOLUME_4]);
            game.spriteBatch.Draw(volume_tex[VOLUME_5], new Rectangle(440, 350, 7, 50), volumeGMusic_color[VOLUME_5]);
        }

        public void drawControls()
        {
            int y = game.graphics.PreferredBackBufferHeight / 2 - NUMBER_OF_BUTTONS / 2 * MENU_BUTTON_HEIGHT - (NUMBER_OF_BUTTONS % 2) * MENU_BUTTON_HEIGHT / 2;
            if(game.graphics.PreferredBackBufferHeight == 768)
                y += 120;
            if (game.graphics.PreferredBackBufferHeight == 600)
                y += 200;
            game.spriteBatch.Draw(controls_tex[CON_ENGINE], new Rectangle(100, y, 140, 30), controls_color[CON_ENGINE]);
            game.spriteBatch.DrawString(font1, Game1.sterowanie.EngineStart.ToString(), new Vector2(270, y+8), Color.Black);
            y += 35;

            game.spriteBatch.Draw(controls_tex[CON_FLYUP], new Rectangle(100, y, 140, 30), controls_color[CON_FLYUP]);
            game.spriteBatch.DrawString(font1, Game1.sterowanie.FlyUp.ToString(), new Vector2(270, y + 8), Color.Black);
            y += 35;
            game.spriteBatch.Draw(controls_tex[CON_FLYDOWN], new Rectangle(100, y, 140, 30), controls_color[CON_FLYDOWN]);
            game.spriteBatch.DrawString(font1, Game1.sterowanie.FlyDown.ToString(), new Vector2(270, y + 8), Color.Black);
            y += 35;
            game.spriteBatch.Draw(controls_tex[CON_FLYFORWARD], new Rectangle(100, y, 140, 30), controls_color[CON_FLYFORWARD]);
            game.spriteBatch.DrawString(font1, Game1.sterowanie.FlyForward.ToString(), new Vector2(270, y + 8), Color.Black);
            y += 35;
            game.spriteBatch.Draw(controls_tex[CON_FLYBACKWARD], new Rectangle(100, y, 140, 30), controls_color[CON_FLYBACKWARD]);
            game.spriteBatch.DrawString(font1, Game1.sterowanie.FlyBackward.ToString(), new Vector2(270, y + 8), Color.Black);
            y += 35;
            game.spriteBatch.Draw(controls_tex[CON_FLYLEFT], new Rectangle(100, y, 140, 30), controls_color[CON_FLYLEFT]);
            game.spriteBatch.DrawString(font1, Game1.sterowanie.FlyLeft.ToString(), new Vector2(270, y + 8), Color.Black);
            y += 35;
            game.spriteBatch.Draw(controls_tex[CON_FLYRIGHT], new Rectangle(100, y, 140, 30), controls_color[CON_FLYRIGHT]);
            game.spriteBatch.DrawString(font1, Game1.sterowanie.FlyRight.ToString(), new Vector2(270, y + 8), Color.Black);
            y += 35;
            game.spriteBatch.Draw(controls_tex[CON_SHOOT], new Rectangle(100, y, 140, 30), controls_color[CON_SHOOT]);
            game.spriteBatch.DrawString(font1, Game1.sterowanie.ShootMissile.ToString(), new Vector2(270, y + 8), Color.Black);
            y += 100;

            game.spriteBatch.Draw(button_tex[EXIT_BUTTON], new Rectangle(100, y,140,59), button_color[EXIT_BUTTON]);
        }

        public void initialize() { 
            if(!resApplied)
               buttonUp =  SINGLEPLAYER_BUTTON;
            
            audioEngine = new AudioEngine(@"Content\audio\MySounds.xgs");
            waveBank = new WaveBank(audioEngine, @"Content\audio\Wave Bank.xwb");
            soundBank = new SoundBank(audioEngine, @"Content\audio\Sound Bank.xsb");
            bgrMusic = soundBank.GetCue("airwolf");

            int x = game.graphics.PreferredBackBufferWidth / 2 - (MENU_BUTTON_WIDTH / 2);
            int y = game.graphics.PreferredBackBufferHeight / 2 - NUMBER_OF_BUTTONS / 2 * MENU_BUTTON_HEIGHT - (NUMBER_OF_BUTTONS % 2) * MENU_BUTTON_HEIGHT / 2;
            if(game.graphics.PreferredBackBufferHeight == 768)
                y += 120;
            if (game.graphics.PreferredBackBufferHeight == 600)
                y += 200;


            for (int i = 0; i < NUMBER_OF_BUTTONS; i++)
            {

                button_color[i] = Color.White;
                button_rectangle[i] = new Rectangle(x, y, MENU_BUTTON_WIDTH, MENU_BUTTON_HEIGHT);
                y += MENU_BUTTON_HEIGHT;
            }

            for (int i = 0; i < 8; i++)
                controls_color[i] = Color.White;

            for (int i = 0; i < 3; i++)
            {
                volumeSound_color[i] = Color.Red;
            }
            volumeSound_color[VOLUME_4] = Color.White;
            volumeSound_color[VOLUME_5] = Color.White;

            for (int i = 0; i < 3; i++)
            {
                volumeMMusic_color[i] = Color.Red;
            }
            volumeMMusic_color[VOLUME_4] = Color.White;
            volumeMMusic_color[VOLUME_5] = Color.White;

            for (int i = 0; i < 3; i++)
            {
                volumeGMusic_color[i] = Color.Red;
            }
            volumeGMusic_color[VOLUME_4] = Color.White;
            volumeGMusic_color[VOLUME_5] = Color.White;
            if(!resApplied)
              button_color[SINGLEPLAYER_BUTTON] = Color.Yellow;
            
            volumeLevelSound = VOLUME_3;
            volumeLevelMMusic = VOLUME_3;
            volumeLevelGMusic = VOLUME_3;

            soundCategory = audioEngine.GetCategory("Sound");
            musicCategory = audioEngine.GetCategory("Music");
            gamemusicCategory = audioEngine.GetCategory("IngameMusic");

            soundCategory.SetVolume(soundVolume);
            musicCategory.SetVolume(musicVolume);
            gamemusicCategory.SetVolume(gameVolume);
            //float vol = audioCategory.GetVolume();


            oldKeybState = Keyboard.GetState();
        
        }


        public void draw(GameTime gameTime)
        {
            game.spriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Immediate, SaveStateMode.SaveState);
            
            if (initial || resApplied) {
        
                initialize();
                bgrMusic.Play();
                initial = false;
                resApplied = false;
            }

            

            if (changeScreen == 1)
            {
                game.spriteBatch.Draw(tex, new Rectangle(0, 0, game.graphics.PreferredBackBufferWidth, game.graphics.PreferredBackBufferHeight), Color.White);
                for (int i = 0; i < 4; i++)
                    game.spriteBatch.Draw(button_tex[i], button_rectangle[i], button_color[i]);
                
            }

            if (changeScreen == 2)
            {
                game.spriteBatch.Draw(tex2, new Rectangle(0, 0, game.graphics.PreferredBackBufferWidth, game.graphics.PreferredBackBufferHeight), Color.White);
                drawSingle();
            }


            if (changeScreen == 3)
            {
                game.spriteBatch.End();
                if (game.network != null) {
                    return;
                }
                TestState testState = game.sm.pobierzStan("Sandbox") as TestState;
                testState.Multiplayer = true;

                game.network = new Network(game, testState, testState.helikopter1);
                testState.network = game.network;
                game.network.JoinSession();

                soundBank.PlayCue("badbang");
                bgrMusic.Pause();
                Game1.stan = "Sandbox";
                gameMusic = soundBank.GetCue("przyczajka");
                gameMusic.Play();



                game.sm.zmienStan(testState);
                game.spriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Immediate, SaveStateMode.SaveState);
                
            }

            if (changeScreen == 4)
            {
                game.spriteBatch.Draw(tex2, new Rectangle(0, 0, game.graphics.PreferredBackBufferWidth, game.graphics.PreferredBackBufferHeight), Color.White);
                drawOptions();
            }

            if (changeScreen == 5)
            {
                game.spriteBatch.Draw(tex2, new Rectangle(0, 0, game.graphics.PreferredBackBufferWidth, game.graphics.PreferredBackBufferHeight), Color.White);
                drawSound();
            }

            if (changeScreen == 6)
            {
                game.spriteBatch.Draw(tex2, new Rectangle(0, 0, game.graphics.PreferredBackBufferWidth, game.graphics.PreferredBackBufferHeight), Color.White);
                drawControls(); 
            }

            game.spriteBatch.End();
        }
    }
}
